package com.study.clothes.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.study.clothes.pojo.Car;
import com.study.clothes.pojo.Order;
import com.study.clothes.pojo.RespBean;
import com.study.clothes.pojo.User;
import com.study.clothes.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private IOrderService iOrderService;
    @GetMapping("/")
    public List<Order> orderList(){

        return iOrderService.list();

    }
    @GetMapping("/getById")
    public List<Order> orderList1(HttpServletRequest request){
        QueryWrapper queryWrapper = new QueryWrapper();
        User userInfo = (User) request.getServletContext().getAttribute("userInfo");
        queryWrapper.eq("userId", userInfo.getId());
        System.out.println(iOrderService.list(queryWrapper).toArray().toString());
        return iOrderService.list(queryWrapper);

    }
    @PostMapping("/")
    public RespBean add(Order order){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("userId", order.getUserId());
        queryWrapper.eq("goodsId", order.getGoodsId());
        Order one = iOrderService.getOne(queryWrapper);
        if (one!=null){
            return RespBean.success("添加成功");
        }
        if (iOrderService.save(order)){
            return RespBean.success("添加成功");
        }
        return RespBean.error("添加失败");

    }
    @PutMapping("/")
    public RespBean update(Order order){
        if (iOrderService.updateById(order)) {
            return RespBean.success("修改成功");
        }
        return RespBean.error("修改失败");
    }
    @DeleteMapping("/{id}/{goodsId}")
    public RespBean deleteOrder(@PathVariable Integer id,@PathVariable Integer goodsId){
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("userId", id);
        updateWrapper.eq("goodsId", goodsId);
        if (iOrderService.remove(updateWrapper)) {
            return RespBean.success("删除成功");
        }
        return RespBean.error("删除失败");

    }
    @PostMapping("/addMutil")
    public RespBean addMutil(List<Order> orders){
        if (iOrderService.saveBatch(orders)) {
            RespBean.success("添加成功");
        }
        return RespBean.error("添加失败");


    }
    @PutMapping("/update")
    public RespBean update1( Order order){
        System.out.println(order.getGoodsId());
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("goodsId", order.getGoodsId());
        updateWrapper.eq("userId", order.getUserId());
        if (iOrderService.update(order,updateWrapper)) {
            return RespBean.success("修改成功");
        }
        return RespBean.error("修改失败");
    }

}
